//n = 4, a = [4, 1, 3, 2]
n = 5, a = [5, 3, 2, 1, 4]
function solution(n, a) {
    let result = []
    function dfs(a) {
        if (a.length < n) {
            let mid = a.shift()
            a.push(mid)
        }
        if (a.length === 1) {
            result.push(a.shift())
            return
        }
        result.push(a.shift())
        dfs(a)
    }
    dfs(a)
    return result
}
console.log(solution(n, a));
